package wuke.show;

import wuke.service.DBService;
import wuke.service.DBService.LocalBinder;
import wuke.test.R;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.GridView;
import android.widget.ProgressBar;

public class ShowContacts extends Activity {

	private DBService mDbService;
	private DBServiceConnect mDBServiceConnect = new DBServiceConnect();
	private boolean mBound = false;
	private Handler myHandler;
	private ImageAdapter imageAdapter;
	private GridView gv;
	private ProgressBar pb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		//initial myHandler 
		initial();
		findComponets();

	}

	@Override
	protected void onStart() {

		boundService();
		super.onStart();

	}

	@Override
	protected void onStop() {
		if (mBound == true) {
			unbindService(mDBServiceConnect);
		}
		super.onStop();
	}

	private void getContacts() {
		imageAdapter.setmContacts(mDbService.queryContacts());
	}

	

	private void initial() {

		// Request for the progress bar to be shown in the title
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

		setContentView(R.layout.listcontacts);

		// Make sure the progress bar is visible
		setProgressBarVisibility(true);

		myHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {

				ShowContacts.this.imageAdapter.notifyDataSetChanged();
				pb.setVisibility(View.GONE);
			
				gv.setVisibility(View.VISIBLE);
			}

		};

	}

	private void boundService() {

		Intent intent = new Intent(this, DBService.class);
		bindService(intent, mDBServiceConnect, Context.BIND_AUTO_CREATE);

	}

	private void findComponets() {
		gv = (GridView) findViewById(R.id.gridview);
		gv.setNumColumns(3);
		gv.setVisibility(View.INVISIBLE);
		pb = (ProgressBar) findViewById(R.id.progressbar);
		pb.setVisibility(View.VISIBLE);

		imageAdapter = new ImageAdapter(this);
		gv.setAdapter(imageAdapter);

	}

	class DBServiceConnect implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {

			LocalBinder localBinder = (LocalBinder) service;
			mDbService = localBinder.getService();
			mBound = true;


			HandlerThread ht = new HandlerThread("DBQuery");
			ht.start();
			Looper looper = ht.getLooper();

			Myhandler workHandle = new Myhandler(looper);
			Message msg = workHandle.obtainMessage();

			msg.sendToTarget();

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mBound = false;

		}

	}


	class Myhandler extends Handler {

		public Myhandler(Looper looper) {
			super(looper);

		}

		@Override
		public void handleMessage(Message msg) {

			getContacts();
			Message myMsg = myHandler.obtainMessage();
			myMsg.sendToTarget();


		}

	}

}
